// Copyright (c) 2025, 大连理工大学 (Dalian University of Technology)
//
// Licensed under the Mulan PSL v2.
// You can use this file according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of the License at
//
//     http://license.coscl.org.cn/MulanPSL2
//
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. SEE THE MULAN PSL v2
// FOR MORE DETAILS.

/*
 * @Author: qiulin_JXGM ql1746365080@gmail.com
 * @Date: 2024-10-16 09:35:57
 * @LastEditors: qiulin ql1746365080@gmail.com
 * @LastEditTime: 2024-12-12 15:13:59
 * @FilePath: \SGFEM\Import\BDFImport\BulkData\Property\PELASParser.cpp
 */
#include "PELASParser.h"

#include <algorithm>
#include <cstdint>

#include "Utility/Logging/SGLogger.h"

#include "DBManager/DBServiceFactory.h"
#include "DBManager/IService/Input/Property/IPELASService.h"

#include "Import/BDFImport/BDFParserBase.h"

BULKDATA_ENTRY_PARSER_REG (PELAS);

using namespace SG::DBManager;
using namespace SG::Import;
using namespace SG::DataStructure;
/*
// file://./../../Doc/MarkDown/BulkData/PELAS/PELAS.md>
*/
enum PELASkey : std::uint8_t
{
    PELASkey_PID1 = 2,
    PELASkey_K1   = 3,
    PELASkey_GE1  = 4,
    PELASkey_S1   = 5,
    PELASkey_PID2 = 6,
    PELASkey_K2   = 7,
    PELASkey_GE2  = 8,
    PELASkey_S2   = 9,
};
SG::DataStructure::Common::Status BDF::BULKDATA::ParsePELAS (const std::shared_ptr<SG::DBManager::DBServiceFactory>& dbServiceFactory,
                                                             std::vector<std::string>&                               dividedEntryInfo)
{
    // 定义两个临时的PELASData数据结构，tempData1和tempData2
    // TODO(bug 001) m_domainId的正确填写需要确认
    SG::DataStructure::FEM::PELASData tempData1;
    tempData1.m_domainId                        = 1;          // 默认domainId为1
    SG::DataStructure::FEM::PELASData tempData2 = tempData1;  // 复制tempData1到tempData2
    bool                              haveData2 = false;      // 标记是否存在第二组数据

    // 调用divideBulkEntry函数对EntryInfo进行解析，分割为多个字符串

    int curIDKey = 1;  // 用于跟踪当前解析的是第几个字段

    // 遍历分割后的条目信息
    for (auto curStrKey : dividedEntryInfo)
    {
        // 检查当前字符串是否为空（只包含空格）
        bool const isEmpty = is_all_space (curStrKey);
        if (isEmpty)
        {
            curIDKey++;  // 跳过空字段，继续下一个
            continue;
        }

        // 将字符串转换为科学计数法格式

        // 根据当前索引值（curIDKey），解析不同的字段并存储到tempData1或tempData2中
        switch (curIDKey)
        {
        case PELASkey_PID1:
            // 解析PID1字段，存储到tempData1
            tempData1.m_id = convertToI64 (curStrKey);
            break;
        case PELASkey_K1:
            // 解析K1字段，存储到tempData1
            tempData1.m_K = convertToReal (curStrKey);
            break;
        case PELASkey_GE1:
            // 解析GE1字段，存储到tempData1
            tempData1.m_D = convertToReal (curStrKey);
            break;
        case PELASkey_S1:
            // 解析S1字段，存储到tempData1
            tempData1.m_S = convertToReal (curStrKey);
            break;
        case PELASkey_PID2:
            // 标记存在第二组数据，并解析PID2字段，存储到tempData2
            haveData2      = true;
            tempData2.m_id = convertToI64 (curStrKey);
            break;
        case PELASkey_K2:
            // 解析K2字段，存储到tempData2
            tempData2.m_K = convertToReal (curStrKey);
            break;
        case PELASkey_GE2:
            // 解析GE2字段，存储到tempData2
            tempData2.m_D = convertToReal (curStrKey);
            break;
        case PELASkey_S2:
            // 解析S2字段，存储到tempData2
            tempData2.m_S = convertToReal (curStrKey);
            break;
        default:
            // 如果curIDKey不在预期范围内，跳过
            break;
        }

        // 处理完当前字段后，移动到下一个字段
        curIDKey++;
    }

    // 如果存在第二组数据，将tempData2添加到数据库服务中
    if (haveData2)
    {
        dbServiceFactory->get<IPELASService> ()->Add (tempData2);
    }

    // 将tempData1添加到数据库服务中并返回结果
    return dbServiceFactory->get<IPELASService> ()->Add (tempData1);
}